//
//  YFLcommentViewController.m
//  Music
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YFLcommentViewController.h"
#import "YFLcommentTableViewCell.h"
#import "YFLcommentModel.h"
#import "MyTools.h"

@interface YFLcommentViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *sourceArray;
@property(nonatomic,strong) NSMutableArray *hotArray;
@property(nonatomic,strong) NSArray  *list;
@end

@implementation YFLcommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.title = @"评论";
    self.list = [NSArray  arrayWithObjects:@"最热评论",@"最新评论", nil];
    self.view.backgroundColor =[UIColor cyanColor];
    
    [self CreatTableView];
 
    [self getNetData];
    
   [self getHotCommentData];
    NSLog(@"%@",self.singerId);
}


//创建tableView
-(void)CreatTableView
{
   
    self.tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, Screenwidth, Screenheight-64-49) style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
    
    
    [self.tableView registerClass:[YFLcommentTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:self.tableView];
    
    
    
}
//一共两个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.hotArray.count == 0) {
        return 1;
    }
    return 2;
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    YFLcommentTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
    YFLcommentModel *model = [self.sourceArray objectAtIndex:indexPath.row];
    cell.commentModel = model;
    
    return cell;
    
    
}

//分区数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    if (self.hotArray.count == 0) {
        return self.sourceArray.count;
    }if (self.hotArray.count!= 0) {
        if (section == 0) {
            return self.hotArray.count;
        } if (section == 1) {
            return self.sourceArray.count;
        }

    }
    
    
    
    
       return 20;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView  *view =[[ UIView  alloc]initWithFrame:CGRectMake(0, 0, Screenwidth, 20)];
    UILabel  *LAVEL = [[UILabel  alloc]initWithFrame:view.frame];
    if (self.hotArray.count == 0) {
        LAVEL.text = self.list[1];
        
        [view  addSubview:LAVEL];
        
        return view;
    }else{
        LAVEL.text = self.list[section];
        
        [view  addSubview:LAVEL];

        return view;
    }
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 20;
}







//网络获取数据
-(void)getNetData
{
     self.sourceArray =[NSMutableArray array];
    
    //最新评论
    NSString *str =[NSString stringWithFormat:@"http://api.comment.dongting.com/comment/latest?type=singer&subject_id=%@&utdid=V1Uldacj6bcDADkjpiFXHgHN&page=1&size=10&app=ttpod&v=v9.0.2.2016052421&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=8.1&active=1&net=2&openudid=c9b07ac0973c329512858440f1331ada894f0a70&idfa=788F6F0F-81B5-4A98-A2AC-164A37DAA099&utdid=V1Uldacj6bcDADkjpiFXHgHN&alf=(null)&bundle_id=com.ttpod.music&latitude=38.88339002821181&longtitude=121.5446427408854&access_token=(null)&vc=9000200",self.singerId];
    
    
   // NSLog(@"+++++++++++++++++===============%@",str);
    
    
    [MyTools getNetWithUrl:str body:nil header:nil response:LHQJSON success:^(id result) {
        
        NSArray *array =(NSArray *)result;
        
        for (NSDictionary *dic in array) {
            YFLcommentModel *model = [YFLcommentModel new];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.sourceArray addObject:model];
            
        }
        [self.tableView reloadData];
      //  [self CreatTableView];
    } failure:^(NSError *error) {
        
    }];
    
    
}

//获取最热评论
-(void)getHotCommentData
{
    
    
    self.hotArray =[NSMutableArray array];
    
    //最新评论
    NSString *str =[NSString stringWithFormat:@"http://api.comment.dongting.com/comment/hot?type=singer&subject_id=%@&utdid=V1Uldacj6bcDADkjpiFXHgHN&app=ttpod&v=v9.0.2.2016052421&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=8.1&active=1&net=2&openudid=c9b07ac0973c329512858440f1331ada894f0a70&idfa=788F6F0F-81B5-4A98-A2AC-164A37DAA099&utdid=V1Uldacj6bcDADkjpiFXHgHN&alf=(null)&bundle_id=com.ttpod.music&latitude=38.88339002821181&longtitude=121.5446427408854&access_token=(null)&vc=9000200",self.singerId];
    
    
    NSLog(@"++++++++++++++===============%@",str);
    
    
    [MyTools getNetWithUrl:str body:nil header:nil response:LHQJSON success:^(id result) {
        
        NSArray *array =(NSArray *)result;
        
        for (NSDictionary *dic in array) {
            YFLcommentModel *model = [YFLcommentModel new];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.hotArray addObject:model];
            
        }
        
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
